﻿using Sc.Cache;
using Sc.Domain;
using Sc.Persistence;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sc.Service
{
    public class BaseItemService
    {
        /// <summary>
        /// 数据持久层
        /// </summary>
        private BaseItemDAO dao = new BaseItemDAO();
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(BaseItem model)
        {
            ProjectCache.ClearBaseItem();
            return this.dao.Add(model);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(BaseItem model)
        {
            ProjectCache.ClearBaseItem();
            return this.dao.Update(model) ? 1 : 0;
        }
        /// <summary>
        /// 批量删除数据
        /// <param name="idlist">id字符串</param>
        /// </summary>
        public bool DeleteList(string idlist)
        {
            ProjectCache.ClearBaseItem();
            return this.dao.DeleteList(idlist);
        }
         /// <summary>
        /// 获取所有分类明细
        /// </summary>
        /// <returns></returns>
        public IList<BaseItem> GetList()
        {
            var list = ProjectCache.BaseItem;
            IList<BaseItem> result = new List<BaseItem>();
            foreach (var item in list)
            {
                item.Value.BaseItemTypeName = ProjectCache.BaseItemType[item.Value.BaseItemTypeId].Name;
                result.Add(item.Value);
            }
            return result;
        }
        /// <summary>
        /// 根据大类获取明细
        /// </summary>
        /// <param name="id">分类ID</param>
        /// <returns></returns>
        public IList<BaseItem> GetListByType(int id)
        {
            var list = ProjectCache.BaseItem.Where(it => it.Value.BaseItemTypeId == id);
            IList<BaseItem> result = new List<BaseItem>();
            foreach (var item in list)
            {
                result.Add(item.Value);
            }
            return result;
        }
        /// <summary>
        /// 获取大类
        /// </summary>
        /// <returns></returns>
        public IList<BaseItemType> GetBaseItemType()
        {
            var list = ProjectCache.BaseItemType;
            IList<BaseItemType> result = new List<BaseItemType>();
            foreach (var item in list)
            {
                result.Add(item.Value);
            }
            return result;
        }
    }
}
